package co.yixiang.business.demo

import co.yixiang.business.demo.mapper.DemoMapper
import co.yixiang.business.demo.service.IDemo
import co.yixiang.business.demo.vo.DemoVo
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.*
import me.liuwj.ktorm.support.mysql.insertOrUpdate
import org.springframework.stereotype.Service
import javax.annotation.Resource

@Service
open class DemoServiceImp: IDemo, ServiceImpl<DemoMapper, DemoSql>() {

    @Resource
    private lateinit var database: Database

    override fun createData(demoSql: DemoSql): Boolean {


        return this.saveOrUpdate(demoSql)
    }

    override fun getData(demoVo: DemoVo): List<DemoVo> {

        return database.from(Demos).leftJoin(Student, on = Student.id eq Demos.studentId)
            .select(Student.studentName, Demos.title)
            .orderBy(Student.createTime.desc())
            .map {
                DemoVo().apply {
                    title = it[Demos.title]
                    name = it[Student.studentName]
                }
            }.toList()
    }
}
